<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/8/23
 * Time: 14:57
 */
//机构活动
namespace app\mec\controller;
use think\Db;

class Activity extends Base {

    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            if(isset($activity_status)&&$activity_status!="")
            {
                $map['a.activity_status'] = $activity_status;
            }
            if(isset($my_activity_sh)&&$my_activity_sh!="")
            {
                $map['a.activity_sh'] = $my_activity_sh;
            }
            if(isset($key)&&$key!="")
            {
                $map['a.activity_name|um.mechanism_name'] = ['like',"%" . $key . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.create_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.create_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.create_time'] = ['between time',[$start,$end]];
            }
            $map['a.activity_mechanism_id'] = session('mmid');
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_institutional_activities')
                ->alias ('a')
                ->join('up_mechanism um','um.mechanism_id = a.activity_mechanism_id','left')
                ->where($map)
                ->count();//计算总页面
//            $order=input('create_time');//排序方式
//            $list = $BannerModel->getByWhere($order,$page,$limit,$map);

            $list = Db::table('up_institutional_activities')->alias ('a')
                ->join('up_mechanism um','um.mechanism_id = a.activity_mechanism_id','left')
                ->page($page, $limit)
                ->field('a.*,um.mechanism_name')
                ->where($map)
                ->order('a.create_time')
                ->select();
//            foreach ($list as $key=>&$vo){
//                if($vo['activity_mechanism_id']==session('mmid')){
//                    $vo['sh'] = 1;
//                }else{
//                    $vo['sh'] = 2;
//                }
//            }
            foreach ($list as $key=>$vo){
                if($vo['activity_status'] > 1){
                    $num =  Db::table('up_institutional_activities_member')->where('institutional_activities_id',$vo['id'])->count();
                    $list[$key]['num'] = $num;
                }else{
                    $list[$key]['num'] = 0;
                }
                if($vo['activity_img'] != ''){
                    $photo = explode (',',$vo['activity_img']);
                    $list[$key]['picture'] = $photo[0];
                    unset($photo[0]);
                    $list[$key]['activity_img'] = array_values($photo);
                }
            }
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        if(session('?activity_id')){
            session('activity_id',null);
        }
        return $this->fetch('activity/index');
    }

    /**
     * 发布活动
     */
    public function add(){
        if(request()->isAjax()){
            $data['activity_id'] =  ','.implode(',',input('param.activity/a')).',';
            $data['activity_name'] = input('param.activity_name');
            $data['activity_longitude'] = input('param.longitude');
            $data['activity_latitude'] = input('param.latitude');
            $data['activity_address'] = input('param.activity_address');
            $data['activity_sign_start'] = input('param.start');
            $data['activity_sign_end'] = input('param.end');
            $data['activity_start_time'] = input('param.startTime');
            $data['activity_end_time'] = input('param.endTime');
            $start = input('param.start');
            $end = input('param.end');
            $data['activity_time'] = input('param.activity_time');
            $data['activity_content'] = input('param.activity_content');
            $data['activity_mechanism_id'] = session('mmid');//发布机构id
            $data['rang'] = input('param.range');
            $data['activity_sh'] = 3;
            $data['activity_img'] = input('param.images');
            $data['create_time'] = date('Y-m-d H:i:s',time());
            $order = 0.8 +($data['activity_time']*0.2);
            Db::table('up_mechanism')->where('mechanism_id',session('mmid'))->setField('mechanism_sort',$order);
            if($start!=""&&$end!=""){
                $data['is_show'] = 2;
            }else{
                $data['is_show']  =1;
            }
            if($data['rang']==2){
                $data['min'] = input('param.rangMin');
                $data['max'] = input('param.rangMax');
            }
            $data['enrolment'] = input('param.rangeNum');//报名人数

            if($data['enrolment']==2){
                $data['rang_enrolment'] = input('param.member');
            }
            $aaa = Db::table('up_institutional_activities')->insertGetId($data);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }

        $activity = Db::table('up_activity')->select();
        $this->assign('activity',$activity);

        return $this->fetch();
    }
    /***
     * @return mixed
     * 审核
     */
    public function examine(){
//        $InstitutionalactivitiesModel = new InstitutionalactivitiesModel();
//
//        if(request()->isAjax()){
//            $id = input('param.id');
//            $status = input('param.radio1');
//            if($status==2){
//                $data['activity_sh_content'] = input('param.content');
//            }
//            $data['activity_sh'] = $status;
//            $whereMap['id'] = $id;
//            $list = Db::table('up_institutional_activities')->where($whereMap)->update($data);
//            if($list){
//                return json(['code' => 200, 'data' =>"", 'msg' => '审核成功']);
//            }else{
//                return json(['code' => 100, 'data' => '', 'msg' => '审核失败']);
//            }
//        }
//        $id = input('param.id');
//        $where['id'] = $id;
//        $res = $InstitutionalactivitiesModel->getOne($where);
//        $up_activity = Db::table('up_activity')->where('id','IN',$res['activity_id'])->column('activity_name');
//        $mechanism_name = Db::table('up_mechanism')->where('mechanism_id',$res['activity_mechanism_id'])->value('mechanism_name');
//        $res['name'] = implode(',',$up_activity);
//        $res['mechanism_name'] = $mechanism_name;
//        $this->assign('res',$res);
//        return $this->fetch();
    }

    /**
     * 编辑
     */
    public function update(){
        if(request()->isAjax()){
            $whereMap['id'] = input('param.id');
            $data['activity_id'] =  ','.implode(',',input('param.activity/a')).',';
            $data['activity_name'] = input('param.activity_name');
            $data['activity_longitude'] = input('param.longitude');
            $data['activity_latitude'] = input('param.latitude');
            $data['activity_address'] = input('param.activity_address');
            $data['activity_sign_start'] = input('param.start');
            $data['activity_sign_end'] = input('param.end');
            $data['activity_start_time'] = input('param.startTime');
            $data['activity_end_time'] = input('param.endTime');
            $data['activity_time'] = input('param.activity_time');
            $data['activity_content'] = input('param.activity_content');
            $data['range'] = input('param.range');
            if(input('param.images') != ''){
                $data['activity_img'] = input('param.images');
            }
            $data['activity_sh'] = 3;
            if($data['range']==2){
                $data['min'] = input('param.rangMin');
                $data['max'] = input('param.rangMax');
            }
            $data['enrolment'] = input('param.rangeNum');//报名人数
            if($data['enrolment']==2){
                $data['rang_enrolment'] = input('param.member');
            }
            $aaa = Db::table('up_institutional_activities')->where($whereMap)->update($data);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $activity = Db::table('up_activity')->select();
        $id = input('param.id');
        $where['id'] = $id;
        $res = Db::table('up_institutional_activities')->where($where)->find();
        $res['activity_id'] = trim($res['activity_id'],',');
        $res['activity_id'] =explode(',',$res['activity_id']);
        $up_activity = Db::table('up_activity')->where('id','IN',$res['activity_id'])->column('activity_name');
        $mechanism_name = Db::table('up_mechanism')->where('mechanism_id',$res['activity_mechanism_id'])->value('mechanism_name');
        $res['name'] = implode(',',$up_activity);
        $res['mechanism_name'] = $mechanism_name;
        $this->assign('res',$res);
        $this->assign('activity',$activity);
        return $this->fetch();
    }

    /**
     * 查看详情
     */
    public function detail(){
        $activity = Db::table('up_activity')->select();
        $id = input('param.id');
        $where['id'] = $id;
        $res = Db::table('up_institutional_activities')->where($where)->find();
        $res['activity_id'] = trim($res['activity_id'],',');
        $res['activity_id'] =explode(',',$res['activity_id']);
        $up_activity = Db::table('up_activity')->where('id','IN',$res['activity_id'])->column('activity_name');
        $mechanism_name = Db::table('up_mechanism')->where('mechanism_id',$res['activity_mechanism_id'])->value('mechanism_name');
        $res['name'] = implode(',',$up_activity);
        $res['mechanism_name'] = $mechanism_name;
        $this->assign('res',$res);
        $this->assign('activity',$activity);
        return $this->fetch();
    }

    /**
     * 活动报名成员
     */
    public function member(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            if(isset($sign_status)&&$sign_status!="")
            {
//                if($sign_status==1){
                $map['a.sign_status'] = $sign_status;
//                }else{
//                    $map['a.activity_status'] = array('=',2);
//                }

            }
            if(isset($key)&&$key!="")
            {
                $map['uuu.user_username|uuu.user_phone'] = ['like',"%" . $key . "%"];
            }
            $activity_time = Db::table('up_institutional_activities')->where('id',input('param.id'))->value('activity_time');
            $map['a.institutional_activities_id'] = input('param.id');
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_institutional_activities_member')
                ->alias ('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_institutional_activities_member')
                ->alias ('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->field('a.*,uuu.user_username,uuu.user_phone,uuu.user_rel_name')
                ->page($page, $limit)
                ->where($map)
                ->select();//计算总页面
            foreach ($list as $key=>&$vo){


                if(strtotime($vo['sign_end_time'])>0){
                    $time = strtotime($vo['sign_end_time']) - strtotime($vo['sign_time']);
                    if(floor($time/3600) > $activity_time){//如果签到的时长大于活动的举办时长
                        $total_time = $activity_time;
                    }else{
                        $total_time = floor($time/3600);
                    }
                    $vo['total_time'] = $total_time;
                }else{
                    $vo['total_time'] = 0;
                }
            }
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        if(!session('?activity_id')){
            session('activity_id',input('id'));
        }
        return $this->fetch();
    }

    /**
     * 编辑活动报名的签退时间
     */
    public function sign()
    {
        if(request()->isAjax()){
            $id = input('param.id');
            $data['sign_end_time'] = input('param.endTime');
            $res = Db::table('up_institutional_activities_member')->where('id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $sign_end_time = Db::table('up_institutional_activities_member')->where('id',$id)->value('sign_end_time');
        $this->assign('id',$id);
        $this->assign('sign_end_time',$sign_end_time);
        return $this->fetch();
    }
}